﻿@using CubicFWNet.Data.Models
@using CubicFWNet.Data.ViewModal
@model CubicFWNet.Data.Models.OriginalCost
@{
    //ViewBag.Title = "原料成本核算";
    var operationName = ViewBag.OperationName;
    var operationTime = ViewBag.OperationTime;
    var technologyCn = ViewBag.TechnologyCreatorName;//工艺操作人
    var orderProductId = ViewBag.OrderProductId;
    var productName = ViewBag.ProductName;
    var productTechnologyView = ViewBag.ProductTechnologyView as ProductTechnologyView;
    var technologyItemList = ViewBag.TechnologyItemList as List<ProductTechnologyView>;
    var distributeType = ViewBag.DistributeType;
}
<section class="content-header">
    <h1>
        原料成本核算
    </h1>
    <ol class="breadcrumb">
        <li><a href="javascript:void(0);"><i class="fa fa-dashboard"></i>@Resources.Global.PageTitle</a></li>
        <li><a href="@Url.Action("Index", "Home")">首页</a></li>
        <li class="active"><a href="@Url.Action("Check", "Purchase")">原料成本核算</a></li>
    </ol>
</section>
<section class="content">
    <div class="box box-solid">
        <!-- /.box-header -->
        <div class="box-body">
            <div class="row">
                @if (productTechnologyView != null)
                {
                    <form class="form-horizontal" id="frmedit" action="@Url.Action("AddCheck", "Purchase")" method="post">
                        <div class="box-body">
                            <div class="form-group col-md-6">
                                <label for="ProductName" class="col-sm-3 control-label">产品名称</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="ProductName" name="ProductName" value="@productName" readonly="readonly">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">工艺</label>
                                <div class="col-sm-9" style="padding-top: 7px; margin-bottom: 0;">
                                    <a href="javascript:void(0)" class="technologyinfo" data-tid="@productTechnologyView.Id">查看工艺</a>
                                </div>
                            </div>
                            <input type="hidden" id="Id" name="Id" value="@Model.Id" />
                            <input type="hidden" id="OrderProductId" name="OrderProductId" value="@orderProductId" />
                            <input type="hidden" id="technologyCn" name="technologyCn" value="@technologyCn" />
                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div id="json-content">
                                <div class="form-group col-md-6">
                                    <label for="ProductName" class="col-sm-3 control-label">主原材料</label>
                                    <div class="col-sm-9" style="padding-top: 7px; margin-bottom: 0;">
                                        @if (distributeType == 1)
                                        {
                                            <a href="javascript:void(0)" class="productinfo" data-tid="@productTechnologyView.Id" data-mid="@productTechnologyView.ProductId">@productTechnologyView.ProductName</a>
                                        }
                                        else
                                        {
                                            <a href="javascript:void(0)" class="productinfo" data-tid="@productTechnologyView.Id" data-mid="@productTechnologyView.ProductId">@productTechnologyView.ProductName (@productName)</a>
                                        }

                                        @*<input type="text" class="form-control material" name="Material" value="@item.ProductName" readonly="readonly">*@
                                    </div>
                                </div>
                                <div class="form-group col-md-6">
                                    <label for="ProductName" class="col-sm-3 control-label">主料成本(总价)</label>
                                    <div class="col-sm-9">
                                        <input type="text" class="form-control materialPrice okeydown" data-des="@productTechnologyView.ProductId" data-name="@productTechnologyView.ProductName" data-count="1" name="MaterialPrice" value="@productTechnologyView.Price.ToString("f2")" required>
                                    </div>
                                </div>
                                @if (technologyItemList != null)
                                {
                                    foreach (var item in technologyItemList)
                                    {
                                        var amount = (item.Price * item.Count).ToString("f2");
                                        <div class="listProItem-content">
                                            <div class="form-group col-md-6">
                                                <label for="ProductName" class="col-sm-3 control-label">辅料名称</label>
                                                <div class="col-sm-4" style="padding-top: 7px; margin-bottom: 0;">
                                                    <a href="javascript:void(0)" class="productinfo" data-mid="@item.ProductId">@item.ProductName</a>
                                                </div>

                                                <div class="col-sm-5" style="padding-top: 7px; margin-bottom: 0;">
                                                    <span style="font-weight: 700;">数量&nbsp;&nbsp;&nbsp;&nbsp;</span><span>@item.Count</span>
                                                </div>
                                            </div>

                                            <div class="form-group col-md-6">
                                                <label for="ProductName" class="col-sm-3 control-label">辅料成本</label>
                                                <div class="col-sm-9">
                                                    <input type="text" class="form-control materialPrice okeydown" data-des="@item.ProductId" data-name="@item.ProductName" data-count="@item.Count" name="MaterialPrice" value="@amount" required>
                                                </div>
                                            </div>
                                        </div>
                                    }
                                }

                            </div>
                            <div class="form-group col-md-12" style="height: 1px;"></div>
                            <div class="form-group col-md-6">
                                <label class="col-sm-3 control-label">备注</label>
                                <div class="col-sm-9">
                                    <textarea class="form-control" rows="3" placeholder="请输入 ..." name="Memo" id="Memo"></textarea>
                                    <span class="text-red">外购（主料价格）：产品价格*数量</span>
                                </div>
                            </div>
                            <div class="form-group col-md-12" style="height: 1px;">
                            </div>
                            <div class="form-group col-md-6">
                                <label for="operationName" class="col-sm-3 control-label">操作人</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="operationName" name="operationName" value="@operationName" readonly="readonly">
                                </div>
                            </div>
                            <div class="form-group col-md-6">
                                <label for="operationTime" class="col-sm-3 control-label">日期</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="operationTime" name="operationTime" value="@operationTime" readonly="readonly">
                                </div>
                            </div>
                        </div>
                        <div class="box-footer" style="width: 80%; margin-left: 20px;">
                            @if (Model.Status == -1)
                            {
                                <a role="button" href="javascript:void(0)" class="btn btn-primary submitbtn" onclick="formsubmit(1)" id="submitdisabled">保存</a>
                            }
                            else
                            {
                                <a role="button" href="javascript:void(0)" class="btn btn-primary submitbtn" id="updateInfo">修改</a>
                            }

                            <a role="button" href="@Url.Action("Check", "Purchase")" class=" btn btn-primary submitbtn">返回</a>
                            <div class="text-red" style="margin-top:20px;font-weight:bold">下一步：财务管理-->价格核算</div>
                        </div>
                    </form>
                }
            </div>
        </div>
    </div>
</section>
<div id="loadingPage" style="display: none;">
    <div class="loading-shade"></div>
    <div class="loading-content" onclick="$.loading(false)">数据加载中，请稍后…</div>
</div>

<style>
    .has-error {
        border-color: #dd4b39;
        box-shadow: none;
    }
</style>


@section Scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#frmedit").validate({
                submitHandler: function (form) {
                    var postData = $(form).formSerialize();
                    postData.Data = getParam();
                    //postData.Type = type;
                    $.submitForm({
                        url: $(form).attr("action"),
                        param: postData,
                        success: function () {
                            $.modalAlert("操作成功", "success");
                            window.location.href = '@Url.Action("Check", "Purchase")';
                        }
                    });
                }
            });

        });


        $("#updateInfo").click(function () {
            var id = $("#Id").val();
            var memo = $("#Memo").val();
            var data = getParam();
            $.ajax({
                url: "/Purchase/UpdateOriginalCost",
                type: "post",
                data: {
                    id: id,
                    memo: memo,
                    data: data
                },
                success: function (data) {
                    if (data && JSON.parse(data)) {
                        var d = JSON.parse(data);
                        if (d.Code == 200) {
                            $.modalAlert("修改成功", "success");
                            setTimeout(function () {
                                window.location.href = "/Purchase/LookInfo?tId=" + id;
                                //location.reload();
                            }, 1000);
                        } else {
                            $.modalAlert("修改失败", "warning");
                        }
                    } else {
                        $.modalAlert("参数错误", "warning");
                    }
                },
                error: function (xhr, err, state) {
                    //alert("e");
                }
            });
        });


        function formsubmit(t) {
            /*var memo = $("#Memo").val();
            if (memo == "") {
                $.modalAlert("请填写备注", "warning");
                return false;
            }*/
            $("#submitdisabled").attr('disabled', "true");
            if (getParam()) {
                $("#frmedit").submit();
            }
        }

        function getParam() {
            var num = 0;
            $("#json-content").find('.materialPrice').each(function (r) {
                var $this = $(this);
                var v = $this.val();
                if (isNaN(v) || v == "") {
                    num++;
                }
            });
            if (num > 0) {
                $.modalAlert("成本价格格式错误", "warning");
                return false;
            }
            var postdata = [];
            //主键
            $("#json-content").find('.materialPrice').each(function (r) {
                var $this = $(this);
                var _des = $this.attr('data-des');
                var _name = $this.attr('data-name');
                var _count = $this.attr('data-count');
                var value = $this.val() == "" ? "&nbsp;" : $this.val();
                postdata.push({ des: _des, name: _name, value: value, count: _count });
                //switch (type) {
                //    case "checkbox":
                //        postdata.push({ des: _des, name: _name, value: $this.is(":checked") });
                //        break;
                //    case "select":
                //        var value = $this.find("option:selected").val() == "" ? null : $this.find("option:selected").val();
                //        postdata.push({ des: _des, name: _name, value: value });
                //        break;
                //    default:
                //        var value = $this.val() == "" ? "&nbsp;" : $this.val();
                //        postdata.push({ des: _des, name: _name, value: value });
                //        break;
                //}
            });
            return JSON.stringify(postdata);
        }

        $(".okeydown").on("keyup", function () {
            var value = $(this).val();
            if (isNaN(value) || value == "" || value == "Infinity" || value == "NaN") {
                $(this).addClass("has-error");
            } else {
                $(this).removeClass("has-error");
            }
        });


        $(".okeydown").on("keydown", function () {
            var k = event.keyCode;
            if ((k <= 57 && k >= 48) || (k <= 105 && k >= 96) || (k == 8) || (k == 110) || (k == 190)) {
                return true;
            } else {
                return false;
            }
        });

        $(".technologyinfo").click(function () {
            var tid = $(this).data('tid');
            //var mid = $(this).data('mid');
            var options = {
                id: null,
                title: '产品信息',
                width: "100%",
                height: "100%",
                url: "../Technology/TechnologyInfo?tId=" + tid,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        });
        $(".productinfo").click(function () {
            var mid = $(this).data('mid');
            var options = {
                id: null,
                title: '产品信息',
                width: "100%",
                height: "100%",
                url: "../Product/Info?PID=" + mid,
                shade: 0.3,
                btn: ['返回'],
                btnclass: ['btn btn-danger'],
                callBack: null
            }
            $.modalOpen(options);
        });
    </script>
}